package org.example.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.annotation.RequirePermission;
import org.example.domain.User;
import org.example.mapper.UserMapper;
import org.example.response.ApiResponse;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
//@CrossOrigin(origins = "http://localhost:8081")
@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;

//    @RequirePermission("user:view")
    @GetMapping("/list")
    public ApiResponse<Map<String, Object>> getAllUser(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String searchQuery
    ) {
        List<User> userList = userService.selectAllUser(pageNum, pageSize, searchQuery);
        long total = userService.getTotalCount(searchQuery);
        
        // 获取活跃用户数量
        long activeCount = userService.getActiveUserCount();
        
        // 获取新增用户数量
        long newCount = userService.getNewUserCount();
        
        Map<String, Object> result = new HashMap<>();
        result.put("list", userList);
        result.put("total", total);
        result.put("activeCount", activeCount);
        result.put("newCount", newCount);
        
        return ApiResponse.success(result);
    }

    @PostMapping("/register")
    public ApiResponse<User> register(@RequestBody User user) {
        try {
            // 检查用户名是否已存在
            if (userService.existsByUsername(user.getUsername())) {
                return ApiResponse.error(400, "用户名已存在");
            }
            
            // 保存用户
            User savedUser = userService.save(user);
            return ApiResponse.success(savedUser);
        } catch (Exception e) {
            return ApiResponse.error(500, "注册失败: " + e.getMessage());
        }
    }

    /***
     * 更新用户信息
     * @param user
     * @return
     */
    @PutMapping("/update")
    public ApiResponse<String> update(@RequestBody User user) {
        try {
            // 检查用户名是否已存在
            if (userService.existsByUsername(user.getUsername())) {
                return ApiResponse.error(400, "用户名已存在");
            }
            // 保存用户
            int savedUser = userService.update(user);
            return ApiResponse.success("success");
        } catch (Exception e) {
            return ApiResponse.error(500, "更新失败: " + e.getMessage());
        }
    }

    /***
     * 新增
     * @param user
     * @return
     */
    @PostMapping("/add")
    public ApiResponse<String> add(@RequestBody User user) {
        try {
            // 检查用户名是否已存在
            if (userService.existsByUsername(user.getUsername())) {
                return ApiResponse.error(400, "用户名已存在");
            }
            // 保存用户
            int savedUser = userService.add(user);
            return ApiResponse.success("success");
        } catch (Exception e) {
            return ApiResponse.error(500, "新增失败: " + e.getMessage());
        }
    }


    /***
     * 删除
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ApiResponse<String> add(@PathVariable Long id) {
        try {
            // 删除用户
            int flag = userMapper.deleteById(id);
            return ApiResponse.success("success");
        } catch (Exception e) {
            return ApiResponse.error(500, "删除失败: " + e.getMessage());
        }
    }
}
